package com.voicenews.gcm;

import java.util.HashMap;
import java.util.Map;

import com.voicenews.VoiceNewsApplication;
import com.voicenews.R;
import com.voicenews.utils.Utils;
import com.voicenews.webserviceconnect.ICallbackHandler;
import com.voicenews.webserviceconnect.WebServiceConnector;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

@SuppressLint("ShowToast")
public class AutomaticallyArticlesReceiveByCategoryRegister extends Activity implements
		ICallbackHandler {
	private RadioGroup radioTopicGroup;
	private RadioButton radioTopicButton;
	private Button btnOk;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.automatical_news_receiver_by_topic_layout);
		addListenerOnButton();
	}

	public void addListenerOnButton() {

		radioTopicGroup = (RadioGroup) findViewById(R.id.radioTopic);
		btnOk = (Button) findViewById(R.id.btnOk);

		btnOk.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// send registe category to server
				if (Utils.isNetworkAvailable(getApplicationContext())) {
					int selectedId = radioTopicGroup.getCheckedRadioButtonId();

					// find the radiobutton by returned id
					radioTopicButton = (RadioButton) findViewById(selectedId);

					Toast.makeText(AutomaticallyArticlesReceiveByCategoryRegister.this,
							radioTopicButton.getText(), Toast.LENGTH_SHORT).show();
					SharedPreferences setting = getSharedPreferences(
							VoiceNewsApplication.filePrefrences, MODE_PRIVATE);
					String userId = setting.getString("id", "noid");
					Map<String, String> params = new HashMap<String, String>();
					params.put("userId", userId);
					String topicVietnamese = radioTopicButton.getText().toString();

//					if (topicVietnamese.equalsIgnoreCase("Kinh doanh")) {
//						params.put("topic", "Business");
//					} else if (topicVietnamese.equalsIgnoreCase("Thế giới")) {
//						params.put("topic", "International");
//
//					} else if (topicVietnamese.equalsIgnoreCase("Thể thao")) {
//						params.put("topic", "Sport");
//					} else if (topicVietnamese.equalsIgnoreCase("Xã hội")) {
//						params.put("topic", "Society");
//					} else {
//						params.put("topic", "Sport");
//					}
					params.put("topic", topicVietnamese);
					callWebService(params);
				} else {
					Toast.makeText(getApplicationContext(), "no internet", Toast.LENGTH_SHORT).show();
				}

			}
		});

	}

	private void callWebService(Map<String, String> params) {
		WebServiceConnector webServiceConnector = new WebServiceConnector(
				VoiceNewsApplication.hostService + "rest/services/",
				"registerAutomaticallNewsService", params, this,
				WebServiceConnector.GET_METHOD, this);
		webServiceConnector.execute();
	}

	@Override
	public void response(String response) {
		// TODO Auto-generated method stub
		if (response.equalsIgnoreCase("true")) {
			Toast.makeText(getApplicationContext(), "success",
					Toast.LENGTH_SHORT);
		} else {
			Toast.makeText(getApplicationContext(), "fail", Toast.LENGTH_SHORT);
		}
	}

}
